﻿// ===============================================================================
// ServiceLocator.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Kernel
{
    using System;
    using EOS.Framework.Tools.Aspects;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Static service locator container, implemented as singleton.
    /// </summary>
    public class ServiceLocator
    {
        /// <summary>
        /// The current instance of the service locator.
        /// </summary>
        private static IServiceLocator _instance = null;
        /// <summary>
        /// The instance lock.
        /// </summary>
        private static readonly object _instanceLock = new object();

        /// <summary>
        /// Configures the kernel with the specified service locator.
        /// </summary>
        /// <param name="serviceLocator">The service locator. Cannot be null
        /// (Nothing in Visual Basic).</param>
        /// <exception cref="ArgumentNullException"><paramref name="serviceLocator"/>
        /// is null (Nothing in Visual Basic).</exception>
        [ParamNotNull("serviceLocator")]
        public static void Configure(IServiceLocator serviceLocator)
        {
            lock (_instanceLock)
            {
                _instance = serviceLocator;
            }
        }

        /// <summary>
        /// Gets the current service locator instance.
        /// </summary>
        /// <value>The current.</value>
        public static IServiceLocator Current
        {
            get
            {
                if (_instance == null)
                {
                    throw new NotSupportedException(
                        "ServiceLocator was not initialized!");  
                }
                return _instance;
            }
        }

    }
}
